package com.chunuo.aclservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chunuo.aclservice.entity.Permission;
import com.chunuo.aclservice.entity.Role;
import com.chunuo.aclservice.entity.User;
import com.chunuo.aclservice.service.PermissionService;
import com.chunuo.aclservice.service.RolePermissionService;
import com.chunuo.aclservice.service.RoleService;
import com.chunuo.commonutils.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-05
 */
@RestController
@RequestMapping("/aclservice/role")
public class RoleController {

    //分页获取、获取一个、删除一个、批量删除、更新、保存、权限绑定权限、根据权限id获取权限信息

    @Autowired
    private RoleService roleService;
    
    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private PermissionService permissionService;
    
    @ApiOperation("分页获取角色信息")
    @GetMapping("getPageList/{page}/{limit}")
    public R pageList(@PathVariable("page")Long page,
                      @PathVariable("limit")Long limit,
                      Role searchObj){
        IPage pageList = new Page(page,limit);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(searchObj.getRoleName())){
            wrapper.like("role_name",searchObj.getRoleName());
        }
        IPage rolePageList = roleService.page(pageList,wrapper);
        return R.ok().data("items", rolePageList.getRecords()).data("total", rolePageList.getTotal());
    }


    @ApiOperation("获取角色信息")
    @GetMapping("info/{roleId}")
    public R getInfo(@ApiParam("角色Id") @PathVariable("roleId")String roleId){
        Role role = roleService.getById(roleId);
        return R.ok().data("info",role);
    }

    @ApiOperation("删除一个角色")
    @DeleteMapping("delete/{roleId}")
    public R deleteById(@ApiParam("角色Id") @PathVariable("roleId")String roleId){
        roleService.removeById(roleId);
        return R.ok();
    }

    @ApiOperation("根据id批量删除角色")
    @DeleteMapping("delteBatch")
    public R deleteBatch(@ApiParam("List集合的Id") @RequestBody List<String> ids){
        roleService.removeByIds(ids);
        return R.ok();
    }

    @ApiOperation("根据Id更新")
    @PutMapping("updateInfo")
    public R updateById(@ApiParam("根据id更新角色信息") @RequestBody Role role){
        roleService.updateById(role);
        return R.ok();
    }

    @ApiOperation("添加角色")
    @PostMapping("save")
    public R save(@ApiParam("保存的角色信息") @RequestBody Role role){
        roleService.save(role);
        return R.ok();
    }

    @ApiOperation("角色绑定权限")
    @PostMapping("roleBindPermission/{roleId}")
    public R userBindRole(@PathVariable("roleId")String roleId,
                          @RequestBody List<String> permissionIds){
        rolePermissionService.batchBind(roleId,permissionIds);
        return R.ok();
    }

    @ApiOperation("获取角色已绑定权限")
    @GetMapping("getRoleBindPermission/{roleId}")
    public R getUserBindRole(@PathVariable("roleId")String roleId){
        List<Permission> permissionList = permissionService.getRoleBindPermission(roleId);
        return R.ok().data("permissionList",permissionList);
    }


}

